/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.htk.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.htk.entity.HtkSjht;
import com.thinkgem.jeesite.modules.htk.service.HtkSjhtService;

/**
 * 随机话题Controller
 * @author htk
 * @version 2018-04-26
 */
@Controller
@RequestMapping(value = "${adminPath}/htk/htkSjht")
public class HtkSjhtController extends BaseController {

	@Autowired
	private HtkSjhtService htkSjhtService;
	
	@ModelAttribute
	public HtkSjht get(@RequestParam(required=false) String id) {
		HtkSjht entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = htkSjhtService.get(id);
		}
		if (entity == null){
			entity = new HtkSjht();
		}
		return entity;
	}
	
	@RequiresPermissions("htk:htkSjht:view")
	@RequestMapping(value = {"list", ""})
	public String list(HtkSjht htkSjht, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HtkSjht> page = htkSjhtService.findPage(new Page<HtkSjht>(request, response), htkSjht); 
		model.addAttribute("page", page);
		return "modules/htk/htkSjhtList";
	}

	@RequiresPermissions("htk:htkSjht:view")
	@RequestMapping(value = "form")
	public String form(HtkSjht htkSjht, Model model) {
		model.addAttribute("htkSjht", htkSjht);
		return "modules/htk/htkSjhtForm";
	}

	@RequiresPermissions("htk:htkSjht:edit")
	@RequestMapping(value = "save")
	public String save(HtkSjht htkSjht, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, htkSjht)){
			return form(htkSjht, model);
		}
		htkSjhtService.save(htkSjht);
		addMessage(redirectAttributes, "保存话题成功");
		return "redirect:"+Global.getAdminPath()+"/htk/htkSjht/?repage";
	}
	
	@RequiresPermissions("htk:htkSjht:edit")
	@RequestMapping(value = "delete")
	public String delete(HtkSjht htkSjht, RedirectAttributes redirectAttributes) {
		htkSjhtService.delete(htkSjht);
		addMessage(redirectAttributes, "删除话题成功");
		return "redirect:"+Global.getAdminPath()+"/htk/htkSjht/?repage";
	}
	@ResponseBody
	@RequestMapping(value = "/api/sjhtData")
	public List<Map<String, Object>> treeData( HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<HtkSjht> list = htkSjhtService.findList(new HtkSjht());
		for (int i=0; i<list.size(); i++){
			HtkSjht e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", "u_"+e.getId());
			map.put("title", e.getTitle());
			map.put("url", e.getUrl());
			mapList.add(map);
		}
		return mapList;
	}

}